<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="####1. FectDetector人脸的检测方法是用双眼来检测人脸的位置，也就是说无法检测到嘴、侧脸等，双眼必须同时可见，并且眼镜会影响检测的效果。实际上，FaceDetector检测到的并不是人的全脸，而只是双眼。输入图片必须为Bitmap RGB565格式图片太小的话检测不到人脸，小于100x100的图片检测不到人脸，由于Android内存有限，图片太大的话，会出现无法加载图片的异常。">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="Android-FeceDetector(人脸识别)">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/Android-Special-Function/Android-FeceDetector(人脸识别)/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="####1. FectDetector人脸的检测方法是用双眼来检测人脸的位置，也就是说无法检测到嘴、侧脸等，双眼必须同时可见，并且眼镜会影响检测的效果。实际上，FaceDetector检测到的并不是人的全脸，而只是双眼。输入图片必须为Bitmap RGB565格式图片太小的话检测不到人脸，小于100x100的图片检测不到人脸，由于Android内存有限，图片太大的话，会出现无法加载图片的异常。">
<meta property="og:locale" content="default">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/3110861-cae3397203e5c880.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/3110861-41eb6285cf471c0d.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2018-08-06T11:08:24.953Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Android-FeceDetector(人脸识别)">
<meta name="twitter:description" content="####1. FectDetector人脸的检测方法是用双眼来检测人脸的位置，也就是说无法检测到嘴、侧脸等，双眼必须同时可见，并且眼镜会影响检测的效果。实际上，FaceDetector检测到的并不是人的全脸，而只是双眼。输入图片必须为Bitmap RGB565格式图片太小的话检测不到人脸，小于100x100的图片检测不到人脸，由于Android内存有限，图片太大的话，会出现无法加载图片的异常。">
<meta name="twitter:image" content="http://upload-images.jianshu.io/upload_images/3110861-cae3397203e5c880.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/Android-Special-Function/Android-FeceDetector(人脸识别)/"/>





  <title>Android-FeceDetector(人脸识别) | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/Android-Special-Function/Android-FeceDetector(人脸识别)/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Android-FeceDetector(人脸识别)</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:38+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>####1. FectDetector<br>人脸的检测方法是用双眼来检测人脸的位置，也就是说无法检测到嘴、侧脸等，双眼必须同时可见，并且眼镜会影响检测的效果。实际上，FaceDetector检测到的并不是人的全脸，而只是双眼。输入图片必须为Bitmap RGB565格式<br>图片太小的话检测不到人脸，小于100x100的图片检测不到人脸，由于Android内存有限，图片太大的话，会出现无法加载图片的异常。<br><a id="more"></a></p>
<p>检测到的人脸存放到FaceDetector.Face类中，该类无法再扩展。从该类可以获取到人眼的中心位置和双眼之间的具体。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br></pre></td><td class="code"><pre><span class="line">public class MainActivity extends AppCompatActivity &#123;</span><br><span class="line">  private static final String TAG = &quot;MainActivity&quot;;</span><br><span class="line"></span><br><span class="line">  @Override protected void onCreate(Bundle savedInstanceState) &#123;</span><br><span class="line">    super.onCreate(savedInstanceState);</span><br><span class="line"></span><br><span class="line">    setContentView(new MyView(this));</span><br><span class="line"></span><br><span class="line">    // 图片路径</span><br><span class="line">    //setContentView(R.layout.activity_main);</span><br><span class="line">    //String path = &quot;sdcard/1.jpg&quot;;</span><br><span class="line">    //String path = &quot;sdcard/2.jpg&quot;;</span><br><span class="line">    //hasFace(path);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 判断图片中是否存在一个人脸</span><br><span class="line">   *</span><br><span class="line">   * @param path 图片路径</span><br><span class="line">   * @return true, 存在人脸; false, 不存在人脸.</span><br><span class="line">   */</span><br><span class="line">  private boolean hasFace(String path) &#123;</span><br><span class="line">    BitmapFactory.Options options = new BitmapFactory.Options();</span><br><span class="line">    // 必须为RGB_565</span><br><span class="line">    options.inPreferredConfig = Bitmap.Config.RGB_565;</span><br><span class="line">    Bitmap bitmap = BitmapFactory.decodeFile(path, options);</span><br><span class="line">    int width = bitmap.getWidth();</span><br><span class="line">    int height = bitmap.getHeight();</span><br><span class="line"></span><br><span class="line">    int numberOfFace = 10;</span><br><span class="line">    FaceDetector.Face[] faces = new FaceDetector.Face[numberOfFace];</span><br><span class="line">    FaceDetector faceDetector = new FaceDetector(width, height, numberOfFace);</span><br><span class="line">    int number = faceDetector.findFaces(bitmap, faces);</span><br><span class="line">    return number != 0;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  private class MyView extends View &#123;</span><br><span class="line">    /** 人脸的数量 */</span><br><span class="line">    private static final int NUMBEROFFACE = 10;</span><br><span class="line">    /** 图片宽度和高度 */</span><br><span class="line">    private int mImageWidth, mImageHeight;</span><br><span class="line">    /** 人脸识别工具类 */</span><br><span class="line">    private FaceDetector myFaceDetect;</span><br><span class="line">    /** 识别到的人脸数组 */</span><br><span class="line">    private FaceDetector.Face[] mFaces;</span><br><span class="line">    /** 眼睛的位置 */</span><br><span class="line">    private float mEyesDistance;</span><br><span class="line">    /** 识别出的人脸总数 */</span><br><span class="line">    private int mNumberOfFaceDetected;</span><br><span class="line">    /** 要识别的图片 */</span><br><span class="line">    private Bitmap mBitmap;</span><br><span class="line"></span><br><span class="line">    public MyView(Context context) &#123;</span><br><span class="line">      super(context);</span><br><span class="line">      // 创建图像参数</span><br><span class="line">      BitmapFactory.Options options = new BitmapFactory.Options();</span><br><span class="line">      // 设置图片涂色格式为RGB_565</span><br><span class="line">      options.inPreferredConfig = Bitmap.Config.RGB_565;</span><br><span class="line">      // 创建一个Bitmap</span><br><span class="line">      mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.wqer, options);</span><br><span class="line">      // 获取图片的宽度</span><br><span class="line">      mImageWidth = mBitmap.getWidth();</span><br><span class="line">      // 获取图片的高度</span><br><span class="line">      mImageHeight = mBitmap.getHeight();</span><br><span class="line">      // 创建人脸数组</span><br><span class="line">      mFaces = new FaceDetector.Face[NUMBEROFFACE];</span><br><span class="line">      // 创建图片解析</span><br><span class="line">      myFaceDetect = new FaceDetector(mImageWidth, mImageHeight, NUMBEROFFACE);</span><br><span class="line">      // 获取人脸的数量</span><br><span class="line">      mNumberOfFaceDetected = myFaceDetect.findFaces(mBitmap, mFaces);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override protected void onDraw(Canvas canvas) &#123;</span><br><span class="line">      // 绘制原图</span><br><span class="line">      canvas.drawBitmap(mBitmap, 0, 0, null);</span><br><span class="line">      // 创建一个抗锯齿的画笔</span><br><span class="line">      Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);</span><br><span class="line">      // 设置画笔的颜色</span><br><span class="line">      paint.setColor(Color.GREEN);</span><br><span class="line">      // 设置画笔的风格，粗体</span><br><span class="line">      paint.setStyle(Paint.Style.STROKE);</span><br><span class="line">      // 设置画笔的宽度</span><br><span class="line">      paint.setStrokeWidth(3);</span><br><span class="line">      // 打印识别出的人脸数量</span><br><span class="line">      Log.e(TAG, mNumberOfFaceDetected + &quot;&quot;);</span><br><span class="line">      // 遍历人脸数组</span><br><span class="line">      for (int i = 0; i &lt; mNumberOfFaceDetected; i++) &#123;</span><br><span class="line">        // 获取相应位置的人脸</span><br><span class="line">        FaceDetector.Face face = mFaces[i];</span><br><span class="line">        // 创建一个长度可为小数的矩形</span><br><span class="line">        PointF pointF = new PointF();</span><br><span class="line">        // 从人脸中获取到中心点</span><br><span class="line">        face.getMidPoint(pointF);</span><br><span class="line">        // 从人脸中获取到眼睛的位置</span><br><span class="line">        mEyesDistance = face.eyesDistance();</span><br><span class="line">        // 打印人脸的位置</span><br><span class="line">        Log.e(TAG, mEyesDistance + &quot;&quot;);</span><br><span class="line">        // 画出矩形</span><br><span class="line">        canvas.drawRect(</span><br><span class="line">            // 左上角横轴坐标</span><br><span class="line">            (int) (pointF.x - mEyesDistance),</span><br><span class="line">            // 左上角纵轴坐标</span><br><span class="line">            (int) (pointF.y - mEyesDistance),</span><br><span class="line">            // 右下角横轴坐标</span><br><span class="line">            (int) (pointF.x + mEyesDistance),</span><br><span class="line">            // 右下角纵轴坐标</span><br><span class="line">            (int) (pointF.y + mEyesDistance),</span><br><span class="line">            // 画笔</span><br><span class="line">            paint);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>效果图:</p>
<p><img src="http://upload-images.jianshu.io/upload_images/3110861-cae3397203e5c880.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="效果图.png"></p>
<p>####2. Play Services Vision<br>1). 导入库</p>
<ul>
<li><p>app/build.gradle文件添加依赖</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">compile &apos;com.google.android.gms:play-services-vision:8.1.0&apos;</span><br></pre></td></tr></table></figure>
</li>
<li><p>AndroidManifest.xml文件中添加</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;meta-data</span><br><span class="line">    android:name=&quot;com.google.android.gms.vision.DEPENDENCIES&quot;</span><br><span class="line">    android:value=&quot;face&quot;/&gt;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>2). 自定义View<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">public class FaceOverlayView extends View &#123;</span><br><span class="line">    private Bitmap mBitmap;</span><br><span class="line">    private SparseArray&lt;Face&gt; mFaceSparseArray;</span><br><span class="line">    public FaceOverlayView(Context context) &#123;</span><br><span class="line">        this(context, null);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public FaceOverlayView(Context context, AttributeSet attrs) &#123;</span><br><span class="line">        this(context, attrs, 0);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public FaceOverlayView(Context context, AttributeSet attrs, int defStyleAttr) &#123;</span><br><span class="line">        super(context, attrs, defStyleAttr);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 设置Bitmap</span><br><span class="line">     * @param bitmap bitmap</span><br><span class="line">     */</span><br><span class="line">    public void setBitmap(Bitmap bitmap) &#123;</span><br><span class="line">        this.mBitmap = bitmap;</span><br><span class="line">        // 创建一个人脸识别工具</span><br><span class="line">        FaceDetector detector =</span><br><span class="line">                new FaceDetector.Builder(getContext())</span><br><span class="line">                .setTrackingEnabled(true)</span><br><span class="line">                .setLandmarkType(FaceDetector.ALL_LANDMARKS)</span><br><span class="line">                .setMode(FaceDetector.FAST_MODE)</span><br><span class="line">                .build();</span><br><span class="line">        // 判断是否可以使用</span><br><span class="line">        if (!detector.isOperational()) &#123;</span><br><span class="line">            // 不可用</span><br><span class="line">            Toast.makeText(getContext(), &quot;不可用&quot;, Toast.LENGTH_SHORT).show();</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            // 根据图片创建一帧</span><br><span class="line">            Frame frame = new Frame.Builder().setBitmap(bitmap).build();</span><br><span class="line">            // 使用工具解析这一帧，获取到人脸的数组</span><br><span class="line">            mFaceSparseArray = detector.detect(frame);</span><br><span class="line">            // 释放工具</span><br><span class="line">            detector.release();</span><br><span class="line">        &#125;</span><br><span class="line">        // 重绘</span><br><span class="line">        invalidate();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    protected void onDraw(Canvas canvas) &#123;</span><br><span class="line">        super.onDraw(canvas);</span><br><span class="line">        if (null != mBitmap &amp;&amp; null != mFaceSparseArray) &#123;</span><br><span class="line">            double scale = drawableBitmap(canvas);</span><br><span class="line">            drawFaceBox(canvas, scale);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 画脸框</span><br><span class="line">     * @param canvas 绘图工具</span><br><span class="line">     * @param scale 缩放工具</span><br><span class="line">     */</span><br><span class="line">    private void drawFaceBox(Canvas canvas, double scale) &#123;</span><br><span class="line">        // 创建画笔</span><br><span class="line">        Paint paint = new Paint();</span><br><span class="line">        // 设置颜色</span><br><span class="line">        paint.setColor(Color.GREEN);</span><br><span class="line">        // 设置风格</span><br><span class="line">        paint.setStyle(Paint.Style.STROKE);</span><br><span class="line">        // 设置线宽2</span><br><span class="line">        paint.setStrokeWidth(5);</span><br><span class="line"></span><br><span class="line">        float left;</span><br><span class="line">        float top;</span><br><span class="line">        float right;</span><br><span class="line">        float bottom;</span><br><span class="line">        // 遍历数组</span><br><span class="line">        for (int i = 0; i &lt; mFaceSparseArray.size(); i++) &#123;</span><br><span class="line">            // 获取对应的脸部信息</span><br><span class="line">            Face face = mFaceSparseArray.valueAt(i);</span><br><span class="line">            // 获取所上角坐标</span><br><span class="line">            left = (float) (face.getPosition().x * scale);</span><br><span class="line">            top = (float) (face.getPosition().y * scale);</span><br><span class="line">            // 获取右下角坐标</span><br><span class="line">            right = (float) (scale * (face.getPosition().x + face.getWidth()));</span><br><span class="line">            bottom = (float) (scale * (face.getPosition().y + face.getHeight()));</span><br><span class="line">            // 画矩形</span><br><span class="line">            canvas.drawRect(left, top, right, bottom, paint);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 绘图</span><br><span class="line">     * @param canvas 绘图工具</span><br><span class="line">     * @return 返回缩放比</span><br><span class="line">     */</span><br><span class="line">    private double drawableBitmap(Canvas canvas) &#123;</span><br><span class="line">        // 获取view的宽高</span><br><span class="line">        double viewWidth = canvas.getWidth();</span><br><span class="line">        double viewHeight = canvas.getHeight();</span><br><span class="line">        // 获取图片宽高</span><br><span class="line">        double imageWidth = mBitmap.getWidth();</span><br><span class="line">        double imageHeight = mBitmap.getHeight();</span><br><span class="line">        // 获取缩放比</span><br><span class="line">        double scale = Math.min(viewWidth / imageWidth, viewHeight / imageHeight);</span><br><span class="line">        // 矩形框</span><br><span class="line">        Rect destBounds = new Rect(0, 0, (int)(imageWidth * scale), (int)(imageHeight * scale));</span><br><span class="line">        // 绘制图片</span><br><span class="line">        canvas.drawBitmap(mBitmap, null, destBounds, null);</span><br><span class="line">        // 返回缩放比</span><br><span class="line">        return scale;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>3). 使用<br>activity.xml<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span><br><span class="line">&lt;RelativeLayout</span><br><span class="line">    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</span><br><span class="line">    xmlns:tools=&quot;http://schemas.android.com/tools&quot;</span><br><span class="line">    android:id=&quot;@+id/activity_main&quot;</span><br><span class="line">    android:layout_width=&quot;match_parent&quot;</span><br><span class="line">    android:layout_height=&quot;match_parent&quot;</span><br><span class="line">    tools:context=&quot;com.mazaiting.visiontest.MainActivity&quot;&gt;</span><br><span class="line"></span><br><span class="line">    &lt;com.mazaiting.visiontest.FaceOverlayView</span><br><span class="line">        android:layout_width=&quot;match_parent&quot;</span><br><span class="line">        android:layout_height=&quot;match_parent&quot;</span><br><span class="line">        android:id=&quot;@+id/face_overlay&quot;</span><br><span class="line">        /&gt;</span><br><span class="line">&lt;/RelativeLayout&gt;</span><br></pre></td></tr></table></figure></p>
<p>MainActivity.java<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">public class MainActivity extends AppCompatActivity &#123;</span><br><span class="line">    @Override</span><br><span class="line">    protected void onCreate(Bundle savedInstanceState) &#123;</span><br><span class="line">        super.onCreate(savedInstanceState);</span><br><span class="line">        setContentView(R.layout.activity_main);</span><br><span class="line">        FaceOverlayView faceOverlayView = (FaceOverlayView) this.findViewById(R.id.face_overlay);</span><br><span class="line"></span><br><span class="line">        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.face);</span><br><span class="line"></span><br><span class="line">        faceOverlayView.setBitmap(bitmap);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>4). 示例图<br><img src="http://upload-images.jianshu.io/upload_images/3110861-41eb6285cf471c0d.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="效果图2.jpg"></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/Android-Special-Function/Android-AES加解密/" rel="next" title="Android-AES加解密">
                <i class="fa fa-chevron-left"></i> Android-AES加解密
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/Android-Special-Function/Android-Fragment懒加载/" rel="prev" title="Android-Fragment懒加载">
                Android-Fragment懒加载 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
